<template>
  <el-dialog :visible.sync="dialog" :title="isAdd ? '新增测试树形数据' : '编辑测试树形数据'" append-to-body width="600px">
    <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" type="text" maxlength="30" style="width:460px" show-word-limit />
      </el-form-item>

      <el-form-item label="测试时间" prop="playDate">
        <el-date-picker
          v-model="form.playDate"
          type="datetime"
          placeholder="选择日期时间"
          default-time="12:00:00"
          style="width:460px"
        />
      </el-form-item>

      <el-form-item label="别名" prop="asName">
        <el-input v-model="form.asName" type="text" maxlength="20" style="width:460px" show-word-limit />
      </el-form-item>

      <el-form-item label="字符串1" prop="str1">
        <el-input v-model="form.str1" type="text" maxlength="10" style="width:460px" show-word-limit />
      </el-form-item>

      <el-form-item label="数字1" prop="num1">
        <el-input-number v-model="form.num1" controls-position="right" style="width:460px" />
      </el-form-item>

      <el-form-item label="小数1" prop="dou1">
        <el-input-number v-model="form.dou1" controls-position="right" style="width:460px" :precision="2" :step="0.1" />
      </el-form-item>
      <el-form-item style="margin-bottom: 0px;" label="上级类目" prop="pid">
        <treeselect v-model="form.pid" :options="treedata" style="width: 460px;" placeholder="选择上级类目" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit, gettreedata } from '@/api/generator/testTree/index'
import { Treeselect } from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: { Treeselect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },

    dictobj: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      dialog: false, loading: false, form: {},
      treedata: [],
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        pid: [{ required: true, message: '上级不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        } else {
          return false
        }
      })
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },

    getParentData() {
      gettreedata().then(res => {
        this.treedata = []
        const parentData = { id: 0, label: '顶级类目', children: [] }
        parentData.children = res.data.tree
        this.treedata.push(parentData)
      })
    },
    resetForm() {
      this.dialog = false
      if (this.$refs['form']) {
        this.$refs['form'].resetFields()
      }
      this.form = {
        playDate: null,
        asName: null,
        str1: null,
        num1: null,
        dou1: null,
        name: null,
        pid: null,
        id: 0
      }
    }
  }
}
</script>

<style scoped>

</style>
